﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class CustomerManagement_AddCustomerTypeChangeManagement : System.Web.UI.Page
{
    private DataSet DstDetails
    {
        get
        {
            DataSet dstDetails = new BLL.CustomerTypeChangeDocDetail().CreateTempDataTable();
            if (ViewState["DstDetails"] != null)
            {
                dstDetails = (DataSet)ViewState["DstDetails"];
            }
            return dstDetails;
        }
        set
        {
            ViewState["DstDetails"] = value;
        }
    }

    private List<Model.CustomerType> CustomerTypes
    {
        get
        {
            List<Model.CustomerType> customerTypes = null;
            if (ViewState["CustomerTypes"] != null)
            {
                customerTypes = (List<Model.CustomerType>)ViewState["CustomerTypes"];
            }
            return customerTypes;
        }
        set
        {
            ViewState["CustomerTypes"] = value;
        }
    }

    private List<Model.V_CARSET> Cards
    {
        get
        {
            List<Model.V_CARSET> cards = null;
            if (ViewState["Cards"] != null)
            {
                cards = (List<Model.V_CARSET>)ViewState["Cards"];
            }
            return cards;
        }
        set
        {
            ViewState["Cards"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("300400108");
        if (!IsPostBack)
        {
            btnCancel.Attributes.Add("onclick", "if(!confirm('要取消新建结算单并返回上一页吗?'))return false;");
        }

        Session["FCode"] = "300400108";
    }

    private void BindDropCardSet(DropDownList dropOriginalLevel, string cardId)
    {
        this.Cards = new BLL.V_CARSET().GetAllList();
        dropOriginalLevel.DataSource = this.Cards;
        dropOriginalLevel.DataTextField = "MC";
        dropOriginalLevel.DataValueField = "DM";
        dropOriginalLevel.DataBind();

        for (int i = 0; i < dropOriginalLevel.Items.Count; i++)
        {
            if (dropOriginalLevel.Items[i].Value == cardId)
            {
                dropOriginalLevel.SelectedIndex = i;
                break;
            }
        }
    }

    private void BindCustomerType(DropDownList drop, string typeId)
    {
        drop.DataSource = this.CustomerTypes;
        drop.DataTextField = "CT_NAME";
        drop.DataValueField = "CT_CODE";
        drop.DataBind();

        for (int i = 0; i < drop.Items.Count; i++)
        {
            if (drop.Items[i].Value == typeId)
            {
                drop.SelectedIndex = i;
                break;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("300400108", "00001"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        try
        {
            SetValueOfDataTable(this.DstDetails);
            if (this.DstDetails.Tables[0].Rows.Count == 0)
            {
                throw new Exception("请添加会员！");

            }
            string newId = "";

            if (new BLL.CustomerTypeChangeDocMain().InsertNew(this.DstDetails, Share.UserValue(0), out newId))
            {
                Share.InsertLog("300400108", "00001");
                Response.Write("<script>alert('新增成功！');window.location.href='CustomerTypeChangeManagementDetail.aspx?code=" + newId + "'</script>");
            }
            else
            {
                Share.Alert(this.Page, "新增未成功，请重新添加！");
            }

        }
        catch (Exception ex)
        {
            Share.Alert(this.Page, ex.Message);
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        //Response.Redirect("LineOfCreditManagement.aspx");
    }

    protected void btnAddDetail_Click(object sender, EventArgs e)
    {
        try
        {
            string customerCode = hidUserCode.Value;
            SetValueOfDataTable(this.DstDetails);
            if (customerCode != "")
            {
                string[] customersArray = customerCode.Split('|');
                if (customersArray.Length > 0)
                {
                    string[] customer;
                    for (int i = 0; i < customersArray.Length; i++)
                    {
                        customer = customersArray[i].Split(' ');
                        if (customer.Length == 2)
                        {


                            Model.V_VIPSET vipCardInfo = new BLL.V_VIPSET().GetModel(customer[0]);
                            if (vipCardInfo == null)
                            {
                                throw new Exception("无法获取会员原始折扣信息！");
                            }
                            DataSet customerInfo= new BLL.CustomerType().GetCustomerTypeByCustomerID(customer[0]);
                            if (customerInfo.Tables[0].Rows.Count == 0)
                            {
                                throw new Exception("系统无法获取会员信息！");
                            }
                            this.CustomerTypes = new BLL.CustomerType().GetAllList();
                            if (this.CustomerTypes == null || this.CustomerTypes.Count == 0)
                            {
                                throw new Exception("系统无法获取会员信息！");
                            }
                            this.DstDetails = new BLL.CustomerTypeChangeDocDetail().AddDataRowToDataTable(this.DstDetails, Convert.ToString(this.DstDetails.Tables[0].Rows.Count + 1), customer[0],
                                customer[1], vipCardInfo.KLDM, vipCardInfo.ZK, customerInfo.Tables[0].Rows[0]["CT_CODE"].ToString(), customerInfo.Tables[0].Rows[0]["CT_Name"].ToString(),
                                this.CustomerTypes[0].CT_CODE, this.CustomerTypes[0].CT_Name, this.CustomerTypes[0].CT_Discount, 0.00M, "");

                        }
                    }
                    dlistDetail.DataSource = this.DstDetails;
                    dlistDetail.DataBind();
                }
            }
            else
            {
                return;//Share.Alert(this.Page, "请选择会员！");
            }
            hidUserCode.Value = "";
        }
        catch (Exception ex)
        {
            Share.Alert(this.Page, ex.Message);
        }
    }

    protected void dlistDetail_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "del")
        {
            if (this.DstDetails != null)
            {
                this.DstDetails.Tables[0].Rows.RemoveAt(e.Item.ItemIndex);
                dlistDetail.DataSource = this.DstDetails;
                dlistDetail.DataBind();
            }
        }
    }

    protected void dlistDetail_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            HtmlInputHidden hidOriginalLevel = (HtmlInputHidden)e.Item.FindControl("hidOriginalLevel");
            DropDownList dropOriginalLevel = (DropDownList)e.Item.FindControl("dropOriginalLevel");
            HtmlInputHidden hidOriginalType = (HtmlInputHidden)e.Item.FindControl("hidOriginalType");
            DropDownList dropOriginalType = (DropDownList)e.Item.FindControl("dropOriginalType");
            HtmlInputHidden hidRegulateType = (HtmlInputHidden)e.Item.FindControl("hidRegulateType");
            DropDownList dropRegulateType = (DropDownList)e.Item.FindControl("dropRegulateType");
            if (dropOriginalLevel != null && hidOriginalLevel != null)
            {
                BindDropCardSet(dropOriginalLevel, hidOriginalLevel.Value);
            }
            if (hidOriginalType != null && dropOriginalType != null)
            {
                BindCustomerType(dropOriginalType, hidOriginalType.Value);
            }
            if (hidRegulateType != null && dropRegulateType != null)
            {
                BindCustomerType(dropRegulateType, hidRegulateType.Value);
            }
        }
    }

    protected void DataList2_DDLCHANGE_Click(object sender, EventArgs e)
    {
        DropDownList dropRegulateType = (DropDownList)sender;
        Label lblRegulateDiscount = (Label)dropRegulateType.Parent.FindControl("lblRegulateDiscount");
        foreach (Model.CustomerType temp in this.CustomerTypes)
        {
            if (dropRegulateType.SelectedValue == temp.CT_CODE)
            {
                lblRegulateDiscount.Text = temp.CT_Discount.ToString();
                break;
            }
        }
    }

    private void SetValueOfDataTable(DataSet dst)
    {
        Label lblDetailId;
        Label lblCustomerCode;
        Label lblCustomerName;
        DropDownList dropOriginalLevel;
        Label lblOriginalDiscount;
        DropDownList dropOriginalType;
        HtmlInputHidden hidOriginalTypeName;
        DropDownList dropRegulateType;
        HtmlInputHidden hidRegulateTypeName;
        Label lblRegulateDiscount;
        TextBox txtAssureAmt;
        TextBox txtWebSiteUrl;

        for (int i = 0; i < dlistDetail.Items.Count; i++)
        {
            lblDetailId = (Label)dlistDetail.Items[i].FindControl("lblDetailId");
            lblCustomerCode = (Label)dlistDetail.Items[i].FindControl("lblCustomerCode");
            lblCustomerName = (Label)dlistDetail.Items[i].FindControl("lblCustomerName");
            dropOriginalLevel = (DropDownList)dlistDetail.Items[i].FindControl("dropOriginalLevel");
            lblOriginalDiscount = (Label)dlistDetail.Items[i].FindControl("lblOriginalDiscount");
            dropOriginalType = (DropDownList)dlistDetail.Items[i].FindControl("dropOriginalType");
            hidOriginalTypeName = (HtmlInputHidden)dlistDetail.Items[i].FindControl("hidOriginalTypeName");
            dropRegulateType = (DropDownList)dlistDetail.Items[i].FindControl("dropRegulateType");
            hidRegulateTypeName = (HtmlInputHidden)dlistDetail.Items[i].FindControl("hidRegulateTypeName");
            lblRegulateDiscount = (Label)dlistDetail.Items[i].FindControl("lblRegulateDiscount");
            txtAssureAmt = (TextBox)dlistDetail.Items[i].FindControl("txtAssureAmt");
            txtWebSiteUrl = (TextBox)dlistDetail.Items[i].FindControl("txtWebSiteUrl");

            dst.Tables[0].Rows[i]["Change_DetailID"] = lblDetailId.Text;
            dst.Tables[0].Rows[i]["C_CustomerID"] = lblCustomerCode.Text;
            dst.Tables[0].Rows[i]["GKMC"] = lblCustomerName.Text;
            dst.Tables[0].Rows[i]["C_OldLevel"] = dropOriginalLevel.SelectedValue;
            dst.Tables[0].Rows[i]["C_OldDiscount"] = lblOriginalDiscount.Text;
            dst.Tables[0].Rows[i]["C_OldType"] = dropOriginalType.SelectedValue;
            dst.Tables[0].Rows[i]["C_OldTypeName"] = hidOriginalTypeName.Value;
            dst.Tables[0].Rows[i]["C_NewType"] = dropRegulateType.SelectedValue;
            dst.Tables[0].Rows[i]["C_NewTypeName"] = hidRegulateTypeName.Value;
            dst.Tables[0].Rows[i]["C_NewDisc"] = Convert.ToDecimal(lblRegulateDiscount.Text.Trim());
            dst.Tables[0].Rows[i]["C_AssureAmt"] = Convert.ToDecimal(txtAssureAmt.Text.Trim());
            dst.Tables[0].Rows[i]["C_WebSiteUrl"] = txtWebSiteUrl.Text;    
        }

    }
}
